<?php
class Rol extends CI_Controller {
		
		function Rol () {
			parent::__construct();
			
			$this->load->helper('url');
			$this->load->helper('form');
			

		}
		
		function index () {
			$data['title']='Rol';
			$data['heading']='Agregar Rol';
			
			$data['result']= $this->getAllOrderBy('nombre');
			$rol = new Entities\Rol;
			$data['registro']=$rol;
			$data['action']='agregar';
			$data['submit']='Agregar';
			$this -> load -> view('rol_view',$data);
		}
		
		public function getAll()
		{
		    $roles = $this->doctrine->em->getRepository('Entities\Rol')->findAll();
			return $roles;
		}
		
		public function getAllOrderBy($orden)
		{
		    //
		    $roles = $this->doctrine->em->createQuery('SELECT p FROM Entities\Rol p ORDER BY p.'. $orden .' ASC')->getResult(); 
			return $roles;
		}
		
		public function remove ($id)
		{
			$rol = $this->doctrine->em->find('Entities\Rol',$id);
			$this->doctrine->em->remove($rol);
			$this->doctrine->em->flush();
			redirect('rol');
		}
		
		public function agregar()
		{
			$rol = new Entities\Rol;
    		$rol->setNombre($_POST["tx_nombre"]);
		
    		$this->doctrine->em->persist($rol);
    		$this->doctrine->em->flush();
    		redirect('rol');
 
		}
		
		public function editar ($id)
		{
			$rol = $this->doctrine->em->find('Entities\Rol',$id);
			$data['title']='Rol';
			$data['heading']='Actualizar Rol';
			
			$data['result']= $this->getAllOrderBy('nombre');
			$data['registro']=$rol;
			$data['action']='actualizar';
			$data['submit']='Actualizar';
			$this -> load -> view('rol_view',$data);
		}
		
		public function actualizar($id)
		{
			$rol = $this->doctrine->em->find('Entities\Rol',$id);
    		$rol->setNombre($_POST["tx_nombre"]);
    		$this->doctrine->em->persist($rol);
    		$this->doctrine->em->flush();
    		redirect('rol');
		}
}
?>